package Model;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import Database.*;
import Helper.*;

public class ProductM extends Model {
    private ArrayList<Product> cache = null;

    public boolean edit(Product p){
        ProductDB db = new ProductDB();

        return db.update(p);
    }

    public boolean remove(int id){
        ProductDB db = new ProductDB();

        return db.delete(id);
    }

    public boolean add(Product p){
        ProductDB db = new ProductDB();

        return db.insert(p);
    }

    public ArrayList<Product> all(){
        init("product");

        return cache;
    }

    public ArrayList<Product> find(int id){
        init("product");

        ArrayList<Product> matches = new ArrayList<>();

        for( Product item : cache ){
            if( item.id == id ){
                matches.add(item);
            }
        }

        return matches;
    }

    public ArrayList<Product> find(String name){
        // override

        DB db = new DB();

        CachedRowSetImpl res = db.select("product", name);

        return createproductArr(res);
    }

    protected void init(String tableName){
        // override

        if( cache == null ){
            DB db = new DB();

            CachedRowSetImpl res = db.get(tableName);

            cache = createproductArr(res);
        }
    }

    private ArrayList<Product> createproductArr(CachedRowSetImpl res){
        // override

        ArrayList<Product> result = new ArrayList<>();

        try {
            while (res.next()) {
                Product p = new Product();
                p.id = res.getInt(1);
                p.name = res.getString(2);
                p.price = res.getFloat(3);
                p.amount = res.getInt(4);

                result.add(p);
            }
        } catch (SQLException exception) {
            System.err.println("数据库出错！");
            if(Helper.isDev){
                exception.printStackTrace();
            }
        }

        return result;
    }
}

class ProductDB extends DB{

    boolean insert(Product p){
        int result = -1;

        try{
            connect();

            String sql = "INSERT INTO product(name,price,amount) VALUES(?,?,?)";
            PreparedStatement statement = dbConnection.prepareStatement(sql);
            statement.setString(1,p.name);
            statement.setFloat(2,p.price);
            statement.setInt(3,p.amount);

            result = statement.executeUpdate();
        }catch(SQLException exception){
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }finally {
            close();
        }

        if( result >= 0 ){
            return true;
        }else{
            return false;
        }
    }

    boolean delete(int id){
        return super.delete("product", id);
    }

    boolean update(Product p){
        int result = -1;

        try{
            connect();

            String sql = "UPDATE product SET name=?,price=?,amount=? WHERE id=?";
            PreparedStatement statement = dbConnection.prepareStatement(sql);
            statement.setString(1, p.name);
            statement.setFloat(2, p.price);
            statement.setInt(3, p.amount);
            statement.setInt(4, p.id);

            result = statement.executeUpdate();
        }catch(SQLException exception){
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }finally {
            close();
        }

        if( result >= 0 ){
            return true;
        }else{
            return false;
        }
    }
}
